const servUser = require('../sever/serveUser')
const servReadTable = require("../sever/serveReadTable")
const md5 = require('md5');
const express = require("express");
const router = express.Router();
const { asynchandler} = require("../router/getResult");
const cry = require("../util/crypt");
const ReadTable = require('../model/readTable');
const servBookList = require("../sever/servBooklist");
const User = require('../model/User');
const { AsyncQueueError } = require('sequelize/dist');
router.post('/login',asynchandler(async (req,res)=>{

    const result = await servUser.getUser(req.body.mobile,req.body.paw);
    if(result){
      const cryId = result.id
      result.id =  cry.encrypto(cryId)
      const value = cry.encrypto(cryId);
      res.cookie("userId",value,{
        path:"/",
        maxAge:'3600000'
      })
      res.header("userId",value);
      return result;
    }
    return null;
}));
//查询某个用户借阅情况，0代表在借书籍，1代表读过的书籍，-1则获取全部默认为-1
router.get("/userReadBook",asynchandler(async (req,res)=>{
    if (!req.query.bookStatus) {
      req.query.bookStatus = -1;
    }
    
    if(req.userId){
      const result = await servReadTable.findUserReadBook(req.userId,req.query.bookStatus,req.query.page); 
      return result;
    }else {
      return  null;
    }
  
} ))
//借书api需要登录拿到cookie和书籍id
router.put("/borrowBook",asynchandler(async (req,res)=>{
  if(!req.userId){
     return null;
  }
  const BookId = req.body.BookId;
  if(!BookId){
    return null;
  }
  const result = await servReadTable.addReadTable(BookId,req.userId);
  return result;
  
 } ))
//从书单里直接借书
router.put("/borrwBookList",asynchandler(async(req,res)=>{
      if(!req.userId){
        return null;
    }
    const BookId = req.body.BookId;
    const result = await servBookList.UpBookList(req.userId,BookId);
    return result;
}))
//获取书单
router.get("/getBookList",asynchandler( async(req,res)=>{
  if(!req.userId){
      return null;
   }
   const result = await servBookList.findUserBookLsit(req.userId);

   return result;

}))
// 添加书单
router.put("/addBookList",asynchandler(async(req,res)=>{
      if(!req.userId){
            return null;
        }
      const BookId = req.body.BookId;
      const re = await servBookList.findBooklistOne(req.userId,BookId);
      if(re.length !== 0){
        console.log(re);
        return null
      }
      const result = await servBookList.addBookList(req.userId,BookId);

        return result;
}))

//还书
router.put("/returnBook",asynchandler(async (req,res)=>{
  if(!req.userId){
    return null;
 }

 const BookId = req.body.BookId;
 if(!BookId){

   return null;
 }
  console.log(req.userId,BookId);
  const result = await servReadTable.updataBookStatus(req.userId,BookId);
  return result;
}))
//更新用户信息
router.put("/userUpdata",asynchandler(async (req,res)=>{
  if(req.cookies.userId){
    let obj = req.body;
    console.log(obj);
    if(obj.paw){
       const opaw = md5(obj.opaw);
       const re = await servUser.isPaw(req.userId);
       console.log("原密码",re)
       if(opaw != re.paw){
         return "404";
       }
       obj.paw = md5(obj.paw);
    }
   
    const result = await servUser.updataUser(obj,req.userId);
    return result;
  }
  return null;
}))
//用户注册
router.post("/addUser",asynchandler(async(req,res)=>{
  const body = req.body;
    if(body.mobile && body.name && body.type && body.birthday && body.paw){

      const result = await servUser.addUser(body.name,body.paw,body.readroot,body.mobile,body.type,body.sex,body.birthday);

        if(result){
            return result;
        }else{
          return null 
        }

    }else{
      return null;
    }
  
} ))
//获取某个用户
router.get("/one",asynchandler(async(req,res)=>{
    const result = await servUser.getUserId(req.userId);
    return result;

}))



module.exports = router;